﻿#region usings
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AR.Entities;
using System.Collections.ObjectModel;
using AR.Model;
using System.ServiceModel.DomainServices.Client;
using AR.Web;
using AR.CustomEntities;
using GalaSoft.MvvmLight.Command;
using AR.Views.Candidat;
using System.Linq;
using AR.Web.Services;
using System.Runtime.InteropServices.Automation;
using System.IO;
using System.Text;
using GalaSoft.MvvmLight;
using System.ComponentModel.DataAnnotations; 
#endregion

namespace AR.ViewModels
{
    public class CSVCandidatViewModel : ViewModelBase
    {
        private readonly CandidatModel model;
        private ObservableCollection<CSVCandidat> _listCandidatsCsv;
        public ObservableCollection<CSVCandidat> ListCandidatsCsv
        {
            get
            {
                return _listCandidatsCsv;
            }
            set
            {
                _listCandidatsCsv = value;
                OnPropertyChanged("ListCandidatsCsv");

            }
        }
        public ObservableCollection<NIVEAU_EXPERIENCE> _niveauxExperience;
        public ObservableCollection<NIVEAU_EXPERIENCE> NiveauxExperience
        {

            get { return _niveauxExperience; }
            set
            {
                _niveauxExperience = value;
                OnPropertyChanged("NiveauxExperience");
                
            }
        }
  
        public ObservableCollection<NIVEAU_ETUDE> _niveauxEtudes;
        private ObservableCollection<CSVCandidat> candidats;
        private ObservableCollection<NIVEAU_ETUDE> NiveauxEtudes1;
        private ObservableCollection<NIVEAU_EXPERIENCE> NiveauxExperience1;
        public ObservableCollection<NIVEAU_ETUDE> NiveauxEtudes
        {


            get { return _niveauxEtudes; }
            set
            {
                _niveauxEtudes = value;
                OnPropertyChanged("NiveauxEtudes");
            }
        }

        public CSVCandidatViewModel(ObservableCollection<CSVCandidat> candidats, ObservableCollection<NIVEAU_ETUDE> NiveauxEtudes, ObservableCollection<NIVEAU_EXPERIENCE> NiveauxExperience)
        {
            model = new CandidatModel();
            this.NiveauxEtudes = NiveauxEtudes;
            this.NiveauxExperience = NiveauxExperience;

            // TODO: Complete member initialization
            
            ListCandidatsCsv = candidats;
            
        }

        


      


        public ICommand SaveAndValiateCommmand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.SaveAndValidate();
                });
            }
        }




        private void SaveAndValidate()
        {
            bool LignesValid = true;
            bool CandidatValid = true;
            string badlignes ="";
            foreach (var Candidat in ListCandidatsCsv)
            {
                CandidatContext context = new CandidatContext();

                        List<ValidationResult> validationResults = new List<ValidationResult>();
                        ValidationContext validationContext = new ValidationContext(Candidat, null, null);
                        CandidatValid = Validator.TryValidateObject(Candidat, validationContext, validationResults, true);
                    if (!CandidatValid)
                    {
                        LignesValid = false;
                        badlignes += Candidat.NumeroLigne.ToString()+",";
                    }
            }
            if (!LignesValid)
            {
                MessageBox.Show("Erreurs dans les lignes suivantes : "+badlignes.Substring(0,badlignes.Length-1)+ " ." + Environment.NewLine + "Double click sur chaque ligne afin de corriger les erreurs .");
            }
            else
            {
                model.AddCandidatCSV(ListCandidatsCsv);
            }
               
            
        }

       
     

    }
}
    
   
